package com.dd;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.chantel.tool.db.DBUtil;
import com.dd.exception.CaiGouBiaoJiaNotFoundException;

public class CaiGouBiaoJia {
	private static final Logger logger = Logger.getLogger(CaiGouBiaoJia.class
			.getName());

	// 采购表id
	private int caiGouBiaoId;

	// 表状态
	private String biaoZhuangTai;

	// 报表名称
	private String mingCheng;

	// 单据编号
	private String danJuBiaoHao;

	// 项目id
	private int xiangMuId;

	// 表类姓名
	private String biaoLeiXingMing;

	// 创建日期
	private Date chuangJianShiJian;

	// 员工ID
	private int yuanGongId;

	// 右边备注
	private String youBianBeiZhu;

	// 下面备注
	private String xiaMianBeiZhu;

	// 项目组意见
	private String xiangMuZouYiJian;

	// 采购部意见
	private String caiGouBuYiJian;

	// 比价意见
	private String biJiaBuYiJian;

	// 中心经理意见
	private String zhongXinJingLiYiJian;

	// 分管中心
	private String fenGuanZongCaiYiJian;

	// 采购公司列表
	private ArrayList caiGouGongSis;

	// 采购商品列表
	private ArrayList caiGouShangPings;

	// 报价
	private float[][] f_product_baojia;

	// 审核价
	private float[][] f_product_shenhejia;

	// 采购表
	private String CaiGouBiaoTable = "CaiGouBiaoTable";

	private String cggs_bjxx = "CaiGouGongSiBaoJiaXinXiTable";

	private String CGBJ_PREFIX_SQL = "select * from CaiGouBiaoTable";

	private String CGBJ_FACTORY_COUNT = "select count(1) as countNum from CaiGouGongSiTable";

	private String CGBJ_PRODUCT_COUNT = "select count(1) as countNum from CaiGouShangPingTable";

	private String CGBJ_GSBAOJIA = "select ShenHeJia, BaoJia from CaiGouGongSiBaoJiaXinXiTable";

	public CaiGouBiaoJia() {
		// do nothing
	}

	public CaiGouBiaoJia(int caiGouBiaoId)
			throws CaiGouBiaoJiaNotFoundException {
		this.caiGouBiaoId = caiGouBiaoId;
		// 根据id从数据库中提取报价单信息
		loadFromDb();
	}

	public CaiGouBiaoJia(String biaoZhuangTai, String mingCheng,
			String danJuBiaoHao, int xiangMuId, String biaoLeiXingMing,
			int yuanGongId, String youBianBeiZhu, String xiaMianBeiZhu,
			String xiangMuZouYiJian, String caiGouBuYiJian,
			String biJiaBuYiJian, String zhongXinJingLiYiJian,
			String fenGuanZongCaiYiJian) {
		super();

		// this.caiGouBiaoId = caiGouBiaoId;
		this.biaoZhuangTai = biaoZhuangTai;
		this.mingCheng = mingCheng;
		this.danJuBiaoHao = danJuBiaoHao;
		this.xiangMuId = xiangMuId;
		this.biaoLeiXingMing = biaoLeiXingMing;
		// this.chuangJianShiJian = chuangJianShiJian;
		this.yuanGongId = yuanGongId;
		this.youBianBeiZhu = youBianBeiZhu;
		this.xiaMianBeiZhu = xiaMianBeiZhu;
		this.xiangMuZouYiJian = xiangMuZouYiJian;
		this.caiGouBuYiJian = caiGouBuYiJian;
		this.biJiaBuYiJian = biJiaBuYiJian;
		this.zhongXinJingLiYiJian = zhongXinJingLiYiJian;
		this.fenGuanZongCaiYiJian = fenGuanZongCaiYiJian;
	}

	public String getBiaoLeiXingMing() {
		return biaoLeiXingMing;
	}

	public void setBiaoLeiXingMing(String biaoLeiXingMing) {
		this.biaoLeiXingMing = biaoLeiXingMing;
	}

	public String getBiaoZhuangTai() {
		return biaoZhuangTai;
	}

	public void setBiaoZhuangTai(String biaoZhuangTai) {
		this.biaoZhuangTai = biaoZhuangTai;
	}

	public String getXiangMuZouYiJian() {
		return xiangMuZouYiJian;
	}

	public void setXiangMuZouYiJian(String xiangMuZouYiJian) {
		this.xiangMuZouYiJian = xiangMuZouYiJian;
	}

	public String getBiJiaBuYiJian() {
		return biJiaBuYiJian;
	}

	public void setBiJiaBuYiJian(String biJiaBuYiJian) {
		this.biJiaBuYiJian = biJiaBuYiJian;
	}

	public int getCaiGouBiaoId() {
		return caiGouBiaoId;
	}

	public void setCaiGouBiaoId(int caiGouBiaoId) {
		this.caiGouBiaoId = caiGouBiaoId;
	}

	public String getCaiGouBuYiJian() {
		return caiGouBuYiJian;
	}

	public void setCaiGouBuYiJian(String caiGouBuYiJian) {
		this.caiGouBuYiJian = caiGouBuYiJian;
	}

	public Date getChuangJianShiJian() {
		return chuangJianShiJian;
	}

	public void setChuangJianShiJian(Date chuangJianShiJian) {
		this.chuangJianShiJian = chuangJianShiJian;
	}

	public String getDanJuBiaoHao() {
		return danJuBiaoHao;
	}

	public void setDanJuBiaoHao(String danJuBiaoHao) {
		this.danJuBiaoHao = danJuBiaoHao;
	}

	public String getFenGuanZongCaiYiJian() {
		return fenGuanZongCaiYiJian;
	}

	public void setFenGuanZongCaiYiJian(String fenGuanZongCaiYiJian) {
		this.fenGuanZongCaiYiJian = fenGuanZongCaiYiJian;
	}

	public String getMingCheng() {
		return mingCheng;
	}

	public void setMingCheng(String mingCheng) {
		this.mingCheng = mingCheng;
	}

	public String getXiaMianBeiZhu() {
		return xiaMianBeiZhu;
	}

	public void setXiaMianBeiZhu(String xiaMianBeiZhu) {
		this.xiaMianBeiZhu = xiaMianBeiZhu;
	}

	public int getXiangMuId() {
		return xiangMuId;
	}

	public void setXiangMuId(int xiangMuId) {
		this.xiangMuId = xiangMuId;
	}

	public String getYouBianBeiZhu() {
		return youBianBeiZhu;
	}

	public void setYouBianBeiZhu(String youBianBeiZhu) {
		this.youBianBeiZhu = youBianBeiZhu;
	}

	public int getYuanGongId() {
		return yuanGongId;
	}

	public void setYuanGongId(int yuanGongId) {
		this.yuanGongId = yuanGongId;
	}

	public String getZhongXinJingLiYiJian() {
		return zhongXinJingLiYiJian;
	}

	public void setZhongXinJingLiYiJian(String zhongXinJingLiYiJian) {
		this.zhongXinJingLiYiJian = zhongXinJingLiYiJian;
	}

	public void PutData(Map m) {
		// if
		m.put("caiGouBiaoId", new Integer(this.caiGouBiaoId));
		//
		m.put("biaoZhuangTai", this.biaoZhuangTai);
		// 报表名称
		m.put("mingCheng", this.mingCheng);
		// 单据编号
		m.put("danJuBiaoHao", this.danJuBiaoHao);
		// 项目id
		m.put("xiangMuId", new Integer(this.xiangMuId));
		// 表类姓名
		m.put("BiaoLeiXingMing", this.biaoLeiXingMing);
		// 创建日期
		m.put("chuangJianShiJian", this.chuangJianShiJian);
		// 员工ID
		m.put("yuanGongId", new Integer(this.yuanGongId));
		// 右边备注
		m.put("youBianBeiZhu", this.youBianBeiZhu);
		// 下面备注
		m.put("xiaMianBeiZhu", this.xiaMianBeiZhu);
		// 项目组意见
		m.put("XiangMuZouYiJian", this.xiangMuZouYiJian);
		// 采购部意见
		m.put("caiGouBuYiJian", this.caiGouBuYiJian);
		// 比价意见
		m.put("biJiaBuYiJian", this.biJiaBuYiJian);
		// 中心经理意见
		m.put("zhongXinJingLiYiJian", this.zhongXinJingLiYiJian);
		// 分管中心
		m.put("fenGuanZongCaiYiJian", this.fenGuanZongCaiYiJian);
	}

	// will add null object check
	public ArrayList GetCaiGouShangPings() {
		if (null != this.caiGouShangPings)
			this.caiGouShangPings.clear();

		this.caiGouShangPings = new ArrayList();

		List li = CaiGouShangPing.GetRecordsFromCaiGouBiaoId(this.caiGouBiaoId);
		Iterator it = li.iterator();
		while (it.hasNext()) {
			Map m = (Map) it.next();

			int shangpingId = ((Integer) m.get("CaiGouShangPingId")).intValue();
			String danWei = (String) m.get("DanWei");
			String guiGe = (String) m.get("GuiGe");
			float liShiJiaGe = ((Double) m.get("LiShiJiaGe")).floatValue();
			String mingCheng = (String) m.get("MingCheng");
			float shuLiang = ((Double) m.get("ShuLiang")).floatValue();

			CaiGouShangPing cgsp = new CaiGouShangPing(this.caiGouBiaoId,
					mingCheng, guiGe, shuLiang, danWei, liShiJiaGe);
			cgsp.setCaiGouShangPingId(shangpingId);

			this.caiGouShangPings.add(cgsp);
		}

		return this.caiGouShangPings;
	}

	public ArrayList GetCaiGouGongSis() {
		if (null != this.caiGouGongSis)
			this.caiGouGongSis.clear();

		this.caiGouGongSis = new ArrayList();

		List li = CaiGouGongSi.GetRecordsFromCaiGouBiaoId(this.caiGouBiaoId);
		Iterator it = li.iterator();

		while (it.hasNext()) {
			Map m = (Map) it.next();

			int gongSiId = ((Integer) m.get("GongSiId")).intValue();
			String gongSimingCheng = (String) m.get("GongSiMingCheng");
			String lianXirenHeFangshi = (String) m.get("LianXirenHeFangshi");
			float zongBaoJia = ((Double) m.get("ZongBaoJia")).floatValue();
			float zongShenHeBaoJia = ((Double) m.get("ZongShenHeBiaoJia"))
					.floatValue();

			CaiGouGongSi cggs = new CaiGouGongSi(this.caiGouBiaoId,
					gongSimingCheng, lianXirenHeFangshi, zongBaoJia,
					zongShenHeBaoJia);
			cggs.setGongSiId(gongSiId);
			this.caiGouGongSis.add(cggs);
		}

		return this.caiGouGongSis;
	}

	public List GetRecords() {
		DBUtil du = DBUtil.getInstance();

		String sql = "select* from CaiGouBiaoTable where CaiGouBiaoId ="
				+ this.caiGouBiaoId;
		List l = du.getRecords(sql);

		return l;
	}

	/**
	 * 执行数据插入 TOD0 error handling 为保证数据完整性，必须在一个事务中处理
	 */
	public boolean InsertCaiGouBiaoJiaDan() {
		// TODO 执行数据插入
		DBUtil du = DBUtil.getInstance();
		Map m = new HashMap();
		// 获取唯一ID，改数据库ID不为自动生成
		this.caiGouBiaoId = SequenceManager.nextID(SysGlobals.CGBJ_ID);
		this.chuangJianShiJian = new java.util.Date();
		PutData(m);

		// 插入主表信息
		boolean bool_res = du.insertRecord(this.CaiGouBiaoTable, m);

		// 插入厂家信息
		Iterator factoryIter = this.caiGouGongSis.iterator();
		int[] cggsids = new int[caiGouGongSis.size()];
		Map m_prd;// = (Map) factoryIter.next();
		int j = 0;
		while (factoryIter.hasNext()) {
			m_prd = (Map) factoryIter.next();
			CaiGouGongSi cggs = new CaiGouGongSi(caiGouBiaoId, (String) m_prd
					.get("GongSiMingCheng"), (String) m_prd
					.get("LianXirenHeFangshi"), ((Float) m_prd
					.get("ZongBaoJia")).floatValue(), 0);
			cggs.InsertGongSi();
			cggsids[j] = cggs.getGongSiId();
			j++;
		}

		// 插入商品信息
		Iterator productIter = this.caiGouShangPings.iterator();
		int k = 0;
		while (productIter.hasNext()) {
			m_prd = (Map) productIter.next();

			CaiGouShangPing cgsp = new CaiGouShangPing(caiGouBiaoId,
					(String) m_prd.get("MingCheng"), (String) m_prd
							.get("GuiGe"), ((Float) m_prd.get("ShuLiang"))
							.floatValue(), (String) m_prd.get("DanWei"),
					((Float) m_prd.get("LiShiJiaGe")).floatValue());
			cgsp.InsertShangPing();

			// 插入各厂家的报价信息
			for (int i = 0; i < f_product_baojia[0].length; i++) {
				Map valueMap = new HashMap();
				valueMap.put("CaiGouShangPingId", new Integer(cgsp
						.getCaiGouShangPingId()));
				valueMap.put("GongSiId", new Integer(cggsids[i]));
				valueMap.put("BaoJia", new Float(f_product_baojia[k][i]));
				du.insertRecord(this.cggs_bjxx, valueMap);
			}
			k++;

		}

		// du.insertRecord(arg0, arg1)
		return bool_res;
	}

	// 执行数据更新
	/**
	 * 为保证数据完整性，必须在一个事务中处理
	 */
	public boolean UpdateCaiGouBiaoJiaDan() {
		// TODO 执行数据更新
		DBUtil du = DBUtil.getInstance();
		Map m = new HashMap();

		PutData(m);

		String updateWhere = "CaiGouBiaoId=" + this.caiGouBiaoId;

		boolean success = du.updateFieldsValue(this.CaiGouBiaoTable, m,
				updateWhere);

		return success;
	}

	/**
	 * 为保证数据完整性，必须在一个事务中处理
	 * 
	 * @param baojiadanId
	 * @return
	 */
	private boolean DeleteBaoJiaDan(int baojiadanId) {
		DBUtil du = DBUtil.getInstance();

		String sql = "delete from CaiGouBiaoTable where CaiGouBiaoId = "
				+ this.caiGouBiaoId;

		boolean bool_res = du.deleteRecords(sql);

		return bool_res;
	}

	// 执行数据删除
	/**
	 * 为保证数据完整性，必须在一个事务中处理
	 */
	public boolean DeleteCaiGouBiaoJiaDan() {
		// TODO 执行数据删除
		DBUtil du = DBUtil.getInstance();

		// Step 1: delete CaiGouGongSiBaoJiaXinXi
		Map m = null;
		int shangpingId = 0;

		List l1 = CaiGouShangPing.GetRecordsFromCaiGouBiaoId(this.caiGouBiaoId);
		Iterator itr = l1.iterator();
		while (itr.hasNext()) {
			m = (Map) itr.next();
			shangpingId = ((BigDecimal) m.get("CaiGouShangPingId")).intValue();

			CaiGouGongSiBaoJiaXinXi.DeleteFromShangPingId(shangpingId);
		}

		int gongSiId = 0;

		List l2 = CaiGouGongSi.GetRecordsFromCaiGouBiaoId(this.caiGouBiaoId);

		itr = l2.iterator();
		while (itr.hasNext()) {
			m = (Map) itr.next();

			gongSiId = ((BigDecimal) m.get("GongSiId")).intValue();

			CaiGouGongSiBaoJiaXinXi.DeleteFromGongsiId(gongSiId);
		}

		// Step 2: delete CaiGouShangPing
		CaiGouShangPing
				.DeleteCaiGouShangPingFromCaiGouBiaoId(this.caiGouBiaoId);

		// Step 3: delete CaiGouGongSi
		CaiGouGongSi.DeleteCaiGouGongSiFromCaiGouBiaoId(this.caiGouBiaoId);

		// Step4 :delete Caigoubaojia
		this.DeleteBaoJiaDan(this.caiGouBiaoId);

		return true;
	}

	private void loadFromDb() throws CaiGouBiaoJiaNotFoundException {
		// TODO 根据ID从数据库中取数据
		DBUtil2 db2 = DBUtil2.getDefaultDBUtil2();
		Map whereFields = new HashMap();
		whereFields.put("CaiGouBiaoId", new Integer(this.caiGouBiaoId));
		Map temp = db2.getRow(CGBJ_PREFIX_SQL, whereFields);
		// this.biaoLeiXingMing
		// this.caiGouBiaoId = caiGouBiaoId;
		this.biaoZhuangTai = (String) temp.get("BiaoZhuangTai");
		this.mingCheng = (String) temp.get("MingCheng");
		this.danJuBiaoHao = (String) temp.get("DanJuBiaoHao");
		this.xiangMuId = ((Integer) temp.get("XiangMuId")).intValue();
		this.biaoLeiXingMing = (String) temp.get("BiaoLeiXingMing");
		this.chuangJianShiJian = (Date) temp.get("ChuangJianShiJian");
		this.yuanGongId = ((Integer) temp.get("YuanGongId")).intValue();
		this.youBianBeiZhu = (String) temp.get("YouBianBeiZhu");
		this.xiaMianBeiZhu = (String) temp.get("XiaMianBeiZhu");
		this.xiangMuZouYiJian = (String) temp.get("XiangMuZouYiJian");
		this.caiGouBuYiJian = (String) temp.get("CaiGouBuYiJian");
		this.biJiaBuYiJian = (String) temp.get("BiJiaBuYiJian");
		this.zhongXinJingLiYiJian = (String) temp.get("ZhongXinJingLiYiJian");
		this.fenGuanZongCaiYiJian = (String) temp.get("FenGuanZongCaiYiJian");

	}

	public float[][] getF_product_baojia() {
		return f_product_baojia;
	}

	public void setF_product_baojia(float[][] f_product_baojia) {
		this.f_product_baojia = f_product_baojia;
	}

	public float[][] getF_product_shenhejia() {
		return f_product_shenhejia;
	}

	public void setF_product_shenhejia(float[][] f_product_shenhejia) {
		this.f_product_shenhejia = f_product_shenhejia;
	}

	public ArrayList getCaiGouGongSis() {
		return caiGouGongSis;
	}

	public void setCaiGouGongSis(ArrayList caiGouGongSis) {
		this.caiGouGongSis = caiGouGongSis;
	}

	public ArrayList getCaiGouShangPings() {
		return caiGouShangPings;
	}

	public void setCaiGouShangPings(ArrayList caiGouShangPings) {
		this.caiGouShangPings = caiGouShangPings;
	}

	// 等待审核的采购表
	public List getDDSHCaiGouBiaos() {
		return getCaiGouBiaosByType(SysGlobals.SH_STATE_DD);
	}

	// 审核通过的采购表
	public List getSHTGCaiGouBiaos() {
		return getCaiGouBiaosByType(SysGlobals.SH_STATE_TJ);
	}

	// 审批拒绝
	public List getSPJJCaiGouBiaos() {
		return getCaiGouBiaosByType(SysGlobals.SH_STATE_JJ);
	}

	// 通过分类取得采购报价列表
	private List getCaiGouBiaosByType(String type) {
		List temp = new ArrayList();
		DBUtil2 db2 = DBUtil2.getDefaultDBUtil2();
		Map whereFields = new HashMap();
		whereFields.put("BiaoZhuangTai", type);
		temp = db2.getRecords(CGBJ_PREFIX_SQL, whereFields);
		return temp;
	}

	public int getCaiGouGongSiCount() {
		DBUtil2 db2 = DBUtil2.getDefaultDBUtil2();
		Map whereFields = new HashMap();
		whereFields.put("CaiGouBiaoId", new Integer(this.caiGouBiaoId));
		Map temp = db2.getRow(this.CGBJ_FACTORY_COUNT, whereFields);
		return ((Integer) temp.get("countNum")).intValue();
	}

	public int getProductsCount() {
		DBUtil2 db2 = DBUtil2.getDefaultDBUtil2();
		Map whereFields = new HashMap();
		whereFields.put("CaiGouBiaoId", new Integer(this.caiGouBiaoId));
		Map temp = db2.getRow(this.CGBJ_PRODUCT_COUNT, whereFields);
		return ((Integer) temp.get("countNum")).intValue();
	}

	public float getGongSiBaoJia(int CaiGouShangPingId, int GongSiId) {
		DBUtil2 db2 = DBUtil2.getDefaultDBUtil2();
		Map whereFields = new HashMap();
		whereFields.put("CaiGouShangPingId", new Integer(CaiGouShangPingId));
		whereFields.put("GongSiId", new Integer(GongSiId));
		Map temp = db2.getRow(this.CGBJ_GSBAOJIA, whereFields);
		Double bj = (Double) temp.get("BaoJia");
		if (bj == null)
			return 0.0f;
		return bj.floatValue();
	}

	public float getShenHeBoaJia(int CaiGouShangPingId, int GongSiId) {
		DBUtil2 db2 = DBUtil2.getDefaultDBUtil2();
		Map whereFields = new HashMap();
		whereFields.put("CaiGouShangPingId", new Integer(CaiGouShangPingId));
		whereFields.put("GongSiId", new Integer(GongSiId));
		Map temp = db2.getRow(this.CGBJ_GSBAOJIA, whereFields);
		Double shj = (Double) temp.get("ShenHeJia");
		if (shj == null)
			return 0.0f;
		return shj.floatValue();
	}

	public static String getDangJuBianHao(int userid) {
		String uid = new String(userid + "");
		StringBuffer sb = new StringBuffer("");
		for (int i = 0; i < 4 - uid.length(); i++) {
			sb.append("0");
		}
		sb.append(uid);
		sb.append("-");
		int cgbjNo = SequenceManager.nextID(SysGlobals.CGBJ_NO);
		String cno = new String(cgbjNo + "");
		for (int i = 0; i < 4 - cno.length(); i++) {
			sb.append("0");
		}
		sb.append(cno);
		return sb.toString();
	}

	public boolean updateShenHeBaoJia(int gongsiID, int shangpingID,
			float shenhejia) {
		DBUtil db = DBUtil.getInstance();
		Map map = new HashMap();
		map.put("ShenHeJia", new Float(shenhejia));
		String sWhere = " CaiGouShangPingId=" + gongsiID + " and GongSiId="
				+ shangpingID;
		return db
				.updateFieldsValue("CaiGouGongSiBaoJiaXinXiTable", map, sWhere);
	}

	public String ShenHeXinXiParse(String xinXi, boolean isContent) {
		// 如果审核信息为空或null
		if (xinXi == null || "".equals(xinXi)) {
			return "未审核"; 
		}
		// 解析审核信息
		// 信息格式：审核内容<$shenhe=审核者姓名$>
		String temp = xinXi;
		String target_str = "<$shenhe=";
		int iindex = temp.indexOf(target_str);
		int iindex2 = temp.indexOf("$>");
		if (iindex2 > iindex) {
			if (isContent)
				return temp.substring(0, iindex);
			else
				return temp.substring(iindex + target_str.length(), iindex2);
		} else
			return "解析出错";
	}
	
	public boolean isShenHeDenDai(){
		if (SysGlobals.SH_STATE_DD.equals(this.biaoZhuangTai))
			return true;
		else
			return false;
	}
	
	public boolean isShenHeTongGou(){
		if (SysGlobals.SH_STATE_TJ.equals(this.biaoZhuangTai))
			return true;
		else
			return false;
	}
	
	public boolean isShenHeJuJue(){
		if (SysGlobals.SH_STATE_JJ.equals(this.biaoZhuangTai))
			return true;
		else
			return false;
	}
}